package pile.customer.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import pile.customer.utils.JwtUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {

    // 签名哈希的密钥，对于不同的加密算法来说含义不同
    public static final String APP_SECRET = "ukc8BDbRigUDaY6pZFfWfs2jZWwewrPHO";

    //获取消息头参数名
    @Value("${token.name}")
    private String tokenName;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        log.info(request.getServletPath());
        //这里要放行路径
        List<String> paths = Arrays.asList(new String[]{
                "/error",
                    "/customerService/passwordLogin","/information/userInformationList","/frequentlyAskedQuestions/userList",
                    "/user/addFile","/information/add","/weChat/receive","/weChat/sendMessage"
                });
        if (paths.contains(request.getServletPath())) {
            return true;
        }
        boolean results = false;
        //获取自营token
        String jwtToken = request.getHeader(tokenName);


        //去一个总的token变量 方便储存自营和第三方token
        String token="";
        if(StringUtils.isNotBlank(jwtToken)){
            token=jwtToken;
        }

        //如果请求头为空 则给token赋值默认值
        if(StringUtils.isBlank(token)){
            token="unintended";
        }
        //调用 checkToken方法判断token是否有效
        results = JwtUtils.checkToken(token, response, request.getServletPath());
        return results;
    }




}

